/**
 * @author Pawel Gadecki <pidzama@gmail.com>
 * 
 */
package spwodne;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import org.jdesktop.layout.GroupLayout;

public class OknoGlowne extends JFrame {

    public OknoGlowne() {
        tm = new MTSpolek();
        int ret = tm.czytajPlik(new File("system.dat"));
        initComponents();
        jTable1.setAutoCreateRowSorter(true);
        SimpleDateFormat sdf = new SimpleDateFormat("d MMM yyyy HH:mm:ss", new Locale("pl", "PL"));
        if (ret == 0) JOptionPane.showMessageDialog(this, (new StringBuilder()).append("Wczytano plik 'system.dat' zapisany: ").append(sdf.format(tm.getDate())).toString());
        else {
            JOptionPane.showMessageDialog(this, "B\u0142ad podczas wczytywania pliku 'system.dat'.");
        }
    }

    private void initComponents() {
        panelSp = new JPanel();
        jButton1 = new JButton();
        jButton2 = new JButton();
        jScrollPane1 = new JScrollPane();
        jTable1 = new JTable(tm);
        jButton3 = new JButton();
        jButton4 = new JButton();
        jButton9 = new JButton();
        jButton10 = new JButton();
        jButton11 = new JButton();
        jButton5 = new JButton();
        jButton6 = new JButton();
        jButton7 = new JButton();
        jButton8 = new JButton();
        setDefaultCloseOperation(3);
        setTitle("Sp\363\u0142ki wodne - Zarząd Spółek Wodnych Gniezno");
        setResizable(false);
        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent evt) {
                formWindowClosing(evt);
            }
            final OknoGlowne this$0;
            

            {
                this$0 = OknoGlowne.this;
            //super();
            }
        });
        panelSp.setBorder(BorderFactory.createEtchedBorder());
        jButton1.setText("Dodaj");
        jButton1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
            final OknoGlowne this$0;
            

            {
                this$0 = OknoGlowne.this;
            //super();
            }
        });
        jButton2.setText("Usu\u0144");
        jButton2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
            final OknoGlowne this$0;
            

            {
                this$0 = OknoGlowne.this;
            //super();
            }
        });
        jTable1.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent evt) {
                jTable1KeyPressed(evt);
            }
            final OknoGlowne this$0;
            

            {
                this$0 = OknoGlowne.this;
            //super();
            }
        });
        jTable1.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
            final OknoGlowne this$0;
            

            {
                this$0 = OknoGlowne.this;
            //super();
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jButton3.setText("Edytuj");
        jButton3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
            final OknoGlowne this$0;
            

            {
                this$0 = OknoGlowne.this;
            //super();
            }
        });
        jButton4.setText("Otw\363rz");
        jButton4.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
            final OknoGlowne this$0;
            

            {
                this$0 = OknoGlowne.this;
            //super();
            }
        });
        jButton9.setText("Dodaj nale\u017Cno\u015B\u0107 za b. rok");
        jButton9.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
            final OknoGlowne this$0;
            

            {
                this$0 = OknoGlowne.this;
            ///super();
            }
        });
        jButton10.setText("Szukaj Klienta");
        jButton10.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
            final OknoGlowne this$0;
            

            {
                this$0 = OknoGlowne.this;
            //super();
            }
        });
        jButton11.setText("Szukaj wp\u0142at");
        jButton11.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
            final OknoGlowne this$0;
            

            {
                this$0 = OknoGlowne.this;
            //super();
            }
        });
        GroupLayout panelSpLayout = new GroupLayout(panelSp);
        panelSp.setLayout(panelSpLayout);
        panelSpLayout.setHorizontalGroup(panelSpLayout.createParallelGroup(1).add(2, panelSpLayout.createSequentialGroup().add(29, 29, 29).add(jScrollPane1, -2, 453, -2).add(panelSpLayout.createParallelGroup(2).add(panelSpLayout.createSequentialGroup().add(76, 76, 76).add(panelSpLayout.createParallelGroup(1).add(2, jButton3, -1, 125, 32767).add(jButton2, -1, 125, 32767).add(2, jButton1, -1, 125, 32767))).add(1, panelSpLayout.createSequentialGroup().addPreferredGap(0).add(panelSpLayout.createParallelGroup(1).add(jButton4, -1, 195, 32767).add(jButton10, -1, 195, 32767).add(jButton9, -1, -1, 32767).add(jButton11, -1, 195, 32767)))).addContainerGap()));
        panelSpLayout.setVerticalGroup(panelSpLayout.createParallelGroup(1).add(panelSpLayout.createSequentialGroup().addContainerGap().add(panelSpLayout.createParallelGroup(1).add(panelSpLayout.createSequentialGroup().add(jScrollPane1, -2, 275, -2).addContainerGap(-1, 32767)).add(panelSpLayout.createSequentialGroup().add(jButton1).addPreferredGap(0).add(jButton3).addPreferredGap(0).add(jButton2).addPreferredGap(0, 66, 32767).add(jButton4).addPreferredGap(0).add(jButton10).addPreferredGap(0).add(jButton9).addPreferredGap(0).add(jButton11).add(16, 16, 16)))));
        jButton5.setText("Wczytaj z pliku");
        jButton5.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
            final OknoGlowne this$0;
            

            {
                this$0 = OknoGlowne.this;
            //super();
            }
        });
        jButton6.setText("Zapisz do pliku");
        jButton6.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
            final OknoGlowne this$0;
            

            {
                this$0 = OknoGlowne.this;
            //super();
            }
        });
        jButton7.setText("Drukuj");
        jButton7.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
            final OknoGlowne this$0;
            

            {
                this$0 = OknoGlowne.this;
            //super();
            }
        });
        jButton8.setText("Zako\u0144cz");
        jButton8.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
            final OknoGlowne this$0;
            

            {
                this$0 = OknoGlowne.this;
            //super();
            }
        });
        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(1).add(layout.createSequentialGroup().addContainerGap().add(panelSp, -2, -1, -2).addContainerGap(-1, 32767)).add(layout.createSequentialGroup().add(43, 43, 43).add(jButton5).add(30, 30, 30).add(jButton6).add(37, 37, 37).add(jButton7).addPreferredGap(0, 156, 32767).add(jButton8).add(35, 35, 35)));
        layout.setVerticalGroup(layout.createParallelGroup(1).add(layout.createSequentialGroup().addContainerGap().add(panelSp, -2, -1, -2).addPreferredGap(0, 24, 32767).add(layout.createParallelGroup(3).add(jButton5).add(jButton6).add(jButton7).add(jButton8)).add(21, 21, 21)));
        pack();
    }

    private void jButton11ActionPerformed(ActionEvent evt) {
        OPodsWpl opwpl = new OPodsWpl();
        opwpl.setVisible(true);
    }

    private void jButton10ActionPerformed(ActionEvent evt) {
        OSzukaj osz = new OSzukaj(tm.getSpL());
        osz.setVisible(true);
    }

    private void jButton9ActionPerformed(ActionEvent evt) {
        if (jTable1.getSelectedRowCount() == 1) {
            int odp = JOptionPane.showConfirmDialog(this, "Czy jestes pewien, \u017Ce chcesz dodac nale\u017Cno\u015B\u0107 za bie\u017C\u0105cy rok wszystkim klientom tej spolki?", "Potwierdzenie", 0);
            if (odp == 0) {
                tm.addNal(jTable1.getSelectedRow());
            }
        }
    }

    private void formWindowClosing(WindowEvent evt) {
        tm.zapiszPilk(new File("system.dat"));
        System.exit(0);
    }

    private void jButton8ActionPerformed(ActionEvent evt) {
        tm.zapiszPilk(new File("system.dat"));
        System.exit(0);
    }

    private void jButton7ActionPerformed(ActionEvent evt) {

        if (jTable1.getSelectedRowCount() > 0) {
            int[] numerki = jTable1.getSelectedRows();
            java.util.List llk = new ArrayList();
            MTSpolek _tmp = tm;
            for (int nr : numerki) {
                for (Iterator i$ = ((Spolka) MTSpolek.getLSpolek().get(nr)).getLWsi().iterator(); i$.hasNext();) {
                    Wies w = (Wies) i$.next();
                    Iterator ii$ = w.getLKli().iterator();
                    while (ii$.hasNext()) {
                        Klient k = (Klient) ii$.next();
                        llk.add(k);
                    }
                }
            }
            OWydruk owd = new OWydruk(llk);
            owd.setVisible(true);
        }
    }

    private void jButton6ActionPerformed(ActionEvent evt) {
        fc.setSelectedFile(new File("data.dat"));
        int returnVal = fc.showSaveDialog(this);
        if (returnVal == 0) {
            File file = fc.getSelectedFile();
            int ret = tm.zapiszPilk(file);
            if (ret < 0) {
                JOptionPane.showMessageDialog(this, (new StringBuilder()).append("B\u0142ad podczas zapisu do pliku. Kod b\u0142edu :").append(ret).toString());
            }
        }
    }

    private void jButton5ActionPerformed(ActionEvent evt) {
        int returnVal = fc.showOpenDialog(this);
        if (returnVal == 0) {
            File file = fc.getSelectedFile();
            int ret = tm.czytajPlik(file);
            SimpleDateFormat sdf = new SimpleDateFormat("d MMM yyyy HH:mm:ss", new Locale("pl", "PL"));
            if (ret == 0) {
                JOptionPane.showMessageDialog(this, (new StringBuilder()).append("Wczytano plik ").append(file.getName()).append(" zapisany: ").append(sdf.format(tm.getDate())).toString());
            } else {
                JOptionPane.showMessageDialog(this, (new StringBuilder()).append("B\u0142ad podczas czytania pliku. Kod b\u0142edu :").append(ret).toString());
            }
        }
    }

    private void jButton4ActionPerformed(ActionEvent evt) {
        if (jTable1.getSelectedRowCount() > 0) {
            openWsie(jTable1.getSelectedRow());
        }
    }

    private void jButton3ActionPerformed(ActionEvent evt) {
        int nr = jTable1.getSelectedRow();
        if (nr >= 0) {
            oesp = new OESp(tm, nr, ((Spolka) tm.getSpL().get(nr)).getNaz(), ((Spolka) tm.getSpL().get(nr)).getStawka().floatValue());
            oesp.setVisible(true);
        }
    }

    private void jTable1MouseClicked(MouseEvent evt) {
        if (evt.getClickCount() == 2) {
            openWsie(jTable1.getSelectedRow());
        }
    }

    private void jTable1KeyPressed(KeyEvent evt) {
        label0:
        {
            int key = evt.getKeyCode();
            if (jTable1.getSelectedRow() < 0) {
                break label0;
            }
            KeyEvent _tmp = evt;
            if (key != 10) {
                KeyEvent _tmp1 = evt;
                if (key != 32) {
                    break label0;
                }
            }
            openWsie(jTable1.getSelectedRow());
        }
    }

    private void jButton2ActionPerformed(ActionEvent evt) {
        if (jTable1.getSelectedRowCount() > 0) {
            int odp = JOptionPane.showConfirmDialog(this, "Czy jestes pewien, \u017Ce chcesz usun\u0105\u0107 wybran\u0105 sp\363\u0142k\u0119 i ca\u0142\u0105 jej zawarto\u015B\u0107?", "Potwierdzenie", 0);
            if (odp == 0) {
                tm.delRow(jTable1.getSelectedRow());
                tm.setProperNumbering();
            }
        }
    }

    private void jButton1ActionPerformed(ActionEvent evt) {
        oesp = new OESp(tm);
        oesp.setVisible(true);
    }

    private void openWsie(int nr) {
        ow = new OWies(((Spolka) tm.getSpL().get(nr)).getLWsi(), ((Spolka) tm.getSpL().get(nr)).getNaz(), nr + 1, ((Spolka) tm.getSpL().get(nr)).getStawka());
        ow.setDefaultCloseOperation(2);
        ow.setVisible(true);
    }
    MTSpolek tm;
    OWies ow;
    final JFileChooser fc = new JFileChooser();
    OESp oesp;
    private JButton jButton1;
    private JButton jButton10;
    private JButton jButton11;
    private JButton jButton2;
    private JButton jButton3;
    private JButton jButton4;
    private JButton jButton5;
    private JButton jButton6;
    private JButton jButton7;
    private JButton jButton8;
    private JButton jButton9;
    private JScrollPane jScrollPane1;
    private JTable jTable1;
    private JPanel panelSp;
}
